package com.ruoyi.zcxt.service.impl;

import java.util.List;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zcxt.constant.ZcxtConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zcxt.mapper.ZcKjSubjectsMapper;
import com.ruoyi.zcxt.domain.ZcKjSubjects;
import com.ruoyi.zcxt.service.IZcKjSubjectsService;

/**
 * 资产用途（树）Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-06
 */
@Service
public class ZcKjSubjectsServiceImpl implements IZcKjSubjectsService 
{
    @Autowired
    private ZcKjSubjectsMapper zcKjSubjectsMapper;

    /**
     * 查询资产用途（树）
     * 
     * @param sId 资产用途（树）主键
     * @return 资产用途（树）
     */
    @Override
    public ZcKjSubjects selectZcKjSubjectsBySId(Long sId)
    {
        return zcKjSubjectsMapper.selectZcKjSubjectsBySId(sId);
    }

    /**
     * 查询资产用途（树）列表
     * 
     * @param zcKjSubjects 资产用途（树）
     * @return 资产用途（树）
     */
    @Override
    public List<ZcKjSubjects> selectZcKjSubjectsList(ZcKjSubjects zcKjSubjects)
    {
        return zcKjSubjectsMapper.selectZcKjSubjectsList(zcKjSubjects);
    }

    /**
     * 新增资产用途（树）
     * 
     * @param zcKjSubjects 资产用途（树）
     * @return 结果
     */
    @Override
    public int insertZcKjSubjects(ZcKjSubjects zcKjSubjects)
    {
        ZcKjSubjects info = zcKjSubjectsMapper.selectZcKjSubjectsBySId(zcKjSubjects.getParentId());
        if (info == null && zcKjSubjects.getParentId() == 0) {
            zcKjSubjects.setAncestors("0");
        }else {
            if (!ZcxtConstants.KJSUBJECTS_NORMAL.equals(info.getStatus())){
                throw new ServiceException("分类停用，不允许新增");
            }
            zcKjSubjects.setAncestors(info.getAncestors() + "," + zcKjSubjects.getParentId());
        }
        zcKjSubjects.setCreateTime(DateUtils.getNowDate());
        return zcKjSubjectsMapper.insertZcKjSubjects(zcKjSubjects);
    }

    /**
     * 修改资产用途（树）
     * 
     * @param zcKjSubjects 资产用途（树）
     * @return 结果
     */
    @Override
    public int updateZcKjSubjects(ZcKjSubjects zcKjSubjects)
    {
        ZcKjSubjects newParentKjSubjects = zcKjSubjectsMapper.selectZcKjSubjectsBySId(zcKjSubjects.getParentId());
        ZcKjSubjects oldKjSubjects = zcKjSubjectsMapper.selectZcKjSubjectsBySId(zcKjSubjects.getsId());
        if (StringUtils.isNotNull(newParentKjSubjects) && StringUtils.isNotNull(oldKjSubjects)){
            String newAncestors = newParentKjSubjects.getAncestors() + "," + newParentKjSubjects.getsId();
            String oldAncestors = oldKjSubjects.getAncestors();
            zcKjSubjects.setAncestors(newAncestors);
            updateKjSubjectsChildren(zcKjSubjects.getsId(), newAncestors, oldAncestors);
        }
        int result = zcKjSubjectsMapper.updateZcKjSubjects(zcKjSubjects);
        if (ZcxtConstants.KJSUBJECTS_NORMAL.equals(zcKjSubjects.getStatus()) && StringUtils.isNotEmpty(zcKjSubjects.getAncestors())
                && !StringUtils.equals("0",zcKjSubjects.getAncestors())){
            // 如果该分类是启用状态，则启用该分类的所有上级分类
            updateParentKjSubjectsStatusNormal(zcKjSubjects);
        }
        zcKjSubjects.setUpdateTime(DateUtils.getNowDate());
        return result;
    }

    /**
     * 批量删除资产用途（树）
     * 
     * @param sIds 需要删除的资产用途（树）主键
     * @return 结果
     */
    @Override
    public int deleteZcKjSubjectsBySIds(Long[] sIds)
    {
        return zcKjSubjectsMapper.deleteZcKjSubjectsBySIds(sIds);
    }

    /**
     * 删除资产用途（树）信息
     * 
     * @param sId 资产用途（树）主键
     * @return 结果
     */
    @Override
    public int deleteZcKjSubjectsBySId(Long sId)
    {
        return zcKjSubjectsMapper.deleteZcKjSubjectsBySId(sId);
    }

    /**
     *  修改子元素关系
     * @param sId 被修改的科目ID
     * @param newAncestors 新的父ID集合
     * @param oldAncestors 旧的父ID集合
     */
    @Override
    public void updateKjSubjectsChildren(Long sId, String newAncestors, String oldAncestors) {
        List<ZcKjSubjects> children = zcKjSubjectsMapper.selectChildrenKjSubjectsById(sId);
        for (ZcKjSubjects child : children){
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors,newAncestors));
        }
        if (children.size() > 0){
            zcKjSubjectsMapper.updateZcKjSubjectsChildren(children);
        }
    }

    /**
     * 修改该分类的父级分类状态
     *
     * @param kjSubjects
     */
    @Override
    public void updateParentKjSubjectsStatusNormal(ZcKjSubjects kjSubjects) {
        String ancestors = kjSubjects.getAncestors();
        Long[] sIds = Convert.toLongArray(ancestors);
        zcKjSubjectsMapper.updateKjSubjectsStatusNormal(sIds);
    }

    @Override
    public int selectNormalChildrenKjSubjectsById(Long sId){
        return zcKjSubjectsMapper.selectNormalChildrenKjSubjectsById(sId);
    }

    @Override
    public boolean hasChildBysId(Long sId){
        int result = zcKjSubjectsMapper.hasChildBysId(sId);
        return result > 0;
    }
}
